<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>
<script>
import echarts from 'echarts'
require('echarts/theme/macarons')
import { debounce } from '@/utils'

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '170px'
    },
    dutyInfo: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      chart: null,
      sum: '',
      inNum: ''
    }
  },
  watch: {
    dutyInfo: {
      handler(newVal, oldVal) {
        // console.log('数据接进来啦', newVal)
        this.sum = newVal.sum
        this.inNum = newVal.inNum
        this.initChart() // 刷新图表
      },
      deep: true
    }
  },
  created() {},
  mounted() {
    // this.initChart()
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption({
        grid: {
          x: 50,
          y: 50,
          x2: 10,
          y2: 20
        },
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b}: {c}'
        },
        graphic: [
          {
            type: 'text',
            left: '46%',
            top: '45%',
            style: {
              text: this.sum,
              textAlign: 'center',
              fill: '#3371a0',
              width: 30,
              height: 30,
              fontSize: 14,
              color: '#4d4f5c',
              fontFamily: 'Microsoft YaHei'
            }
          },
          {
            type: 'text',
            left: '48.5%',
            top: '45%',
            style: {
              text: '/',
              textAlign: 'center',
              fill: '#3371a0',
              width: 30,
              height: 30,
              fontSize: 14,
              color: '#4d4f5c',
              fontFamily: 'Microsoft YaHei'
            }
          },
          {
            type: 'text',
            left: '50%',
            top: '45%',
            style: {
              text: this.inNum,
              textAlign: 'center',
              fill: '#e9e199',
              width: 30,
              height: 30,
              fontSize: 14,
              color: '#4d4f5c',
              fontFamily: 'Microsoft YaHei'
            }
          },
          {
            type: 'text',
            left: '44.5%',
            top: '55%',
            style: {
              text: '实到人数',
              textAlign: 'center',
              fill: '#3371a0',
              width: 30,
              height: 30,
              fontSize: 14
            }
          }
        ],
        legend: {
          y: '55px',
          orient: 'vertical',
          left: '73%',
          itemWidth: 5,
          type: 'scroll',
          itemHeight: 5,
          data: ['出勤人数', '总人数'],
          textStyle: {
            // 图例文字的样式
            color: ['#3371a0', '#e9e199'],
            fontSize: 15
          }
        },
        color: ['#3371a0', '#e9e199'],
        series: [
          {
            name: '访问来源',
            type: 'pie',
            radius: ['50%', '70%'],
            avoidLabelOverlap: false,
            label: {
              show: false,
              position: 'center'
            },
            emphasis: {
              label: {
                show: false,
                fontSize: '30',
                fontWeight: 'bold'
              }
            },
            labelLine: {
              show: false
            },
            data: [
              { value: this.sum, name: '总人数' },
              { value: this.inNum, name: '出勤人数' }
            ]
          }
        ]
      })
    }
  }
}
</script>
